//
//  MaintenceStoreView.swift
//  koolcar
//
//  Created by xiantian on 2023/10/25.
//

import UIKit

class MaintenceStoreView: UIView {
    var itemMenuView: MLMSegmentHead!
    var scrollView: MLMSegmentScroll!
    var categoryArray: [CategoryListModel] = []
    var sketchView: StoreListTableView!
    var viewArray: [StoreListTableView] = []
    override init(frame: CGRect) {
        super.init(frame: frame)
        //self.requestCategoryList()
        self.sketchView = StoreListTableView.init(frame: CGRect.init(x: 0, y: 0, width: frame.size.width, height: frame.size.height), style: .plain)
        self.sketchView.isHidden = false
        self.sketchView.showSkeleton()
        self.addSubview(self.sketchView)
        self.layoutIfNeeded()
    }
    
    //MARK: - Requset
    //商品类型查询接口
    func requestCategoryList(){
        XTRequestCenter.shared.sendRequestMappableTargetObject(target: APIManager.getCategoryList(param: ["level": "1"]), objectType: CategoryModel.self) { data in
            let resultObj = data as! CategoryModel
            if resultObj.code == "1" {
                self.categoryArray = resultObj.data ?? []
                let allModel = CategoryListModel.init()
                allModel.id = -1
                allModel.name = MMLocalizedString("total", comment: "全部")
                self.categoryArray.insert(allModel, at: 0)
            }
            self.initTopMenu(items: self.categoryArray)
            self.willShowSketchView()
            
        } error: { err in
            self.willShowSketchView()
            self.makeToastBottom(message: err?.domain ?? "")
        }
    }
    
    private func willShowSketchView(){
        if self.categoryArray.count > 0 {
            self.sketchView.hideSkeleton()
            self.sketchView.isHidden = true
        }else{
            self.sketchView.isHidden = false
            self.sketchView.hideSkeleton()
            self.sketchView.emptyView.isHidden = false
        }
    }
    
    // MARK: - Privately
    func initTopMenu(items: [CategoryListModel]) {
        self.categoryArray = items
        var titleArra: [String] = []
        for index in 0 ..< self.categoryArray.count{
            let model = self.categoryArray[index]
            titleArra.append(model.name ?? "")
        }
        
        self.itemMenuView = MLMSegmentHead.init(frame: CGRect.init(x: 0, y: 0 , width: kScreenWidth, height: 34), titles: titleArra, headStyle: MLMSegmentHeadStyle.init(1), layoutStyle: MLMSegmentLayoutStyle.init(UInt(2)))
        self.itemMenuView.backgroundColor = UIColor.white
        self.itemMenuView.fontScale = 1
        self.itemMenuView.slideHeight = 1
        self.itemMenuView.lineHeight = 3
        self.itemMenuView.linePaddingBottom = 6
        self.itemMenuView.lineWidth = 24
        self.itemMenuView.fontSize = 14
        self.itemMenuView.unSelectFont = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        self.itemMenuView.selectedFont = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        self.itemMenuView.deSelectColor = Theme.titleLabColor
        self.itemMenuView.headColor = UIColor.clear
        self.itemMenuView.lineColor = Theme.mainColor
        self.itemMenuView.selectColor = Theme.mainColor
        self.itemMenuView.bottomLineHeight = 0
        
        //装所有的tableView
        var viewArray: [StoreListTableView] = []
        for index in 0 ..< titleArra.count{
            let model = self.categoryArray[index]
            let ww: CGFloat =  CGFloat(index) * kScreenWidth
            let tableView = StoreListTableView(frame: CGRect(x: 0, y: 34, width: ww, height: kScreenHeight - NavBarHight - TabbarHeight - 34))
            tableView.backgroundColor = .white
            tableView.isFlashMore = true
            tableView.categoryId = model.id ?? 0
            if index == 0 {
                tableView.sendDataRequest(isMore: false)
            }
            viewArray.append(tableView)
        }
        self.viewArray = viewArray

        self.scrollView = MLMSegmentScroll(frame: CGRect(x: 0, y: 34, width: kScreenWidth, height: kScreenHeight - NavBarHight - TabbarHeight - 34), vcOrViews: (viewArray as [Any]))
        self.scrollView.addScale = 0.1
        self.scrollView.loadAll = false
        self.scrollView.backgroundColor = UIColor.clear
        self.scrollView.showIndex = 0//展示那个
        
        //绑定
        MLMSegmentManager.associateHead(self.itemMenuView, with: self.scrollView, contentChangeAni: true) {
            self.addSubview(self.itemMenuView)
            self.addSubview(self.scrollView)
        } selectEnd: { index in
            guard self.viewArray.count > index else {
                return
            }
            self.viewArray[index].sendDataRequest(isMore: false)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
